﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace App_1Mark1
{
    public partial class Frm_Inscription : Form
    {
        public Frm_Inscription()
        {
            InitializeComponent();
        }

        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand scom = new SqlCommand();
        SqlConnection sqlconnx = new SqlConnection();
        SqlCommandBuilder sqlcb = new SqlCommandBuilder();
        DataSet ds = new DataSet();
        SqlDataReader sdr;
        private void Frm_Inscription_Load(object sender, EventArgs e)
        {
            comb_annee_sc.SelectedIndexChanged -= comb_annee_sc_SelectedIndexChanged;
            sqlconnx.ConnectionString = "Data Source=DESKTOP-03S1896;Initial Catalog=Gestion_Scolarité;Integrated Security=True";
            sqlconnx.Open();
            scom.Connection = sqlconnx;
            scom.CommandText = "select * from Inscription";
            sda.SelectCommand = scom;
            sda.Fill(ds, "Inscription");
            comb_annee_sc.DataSource = ds.Tables["Inscription"];
            comb_annee_sc.ValueMember = "ID_Ins";

            //MessageBox.Show(comb_annee_sc.SelectedValue.ToString());
            scom.CommandText = "select * from Filiere";
            sda.SelectCommand = scom;
            sda.Fill(ds, "Filiere");
            comb_filiere.DataSource = ds.Tables["Filiere"];
            comb_filiere.ValueMember = "ID_Filiere";
            comb_filiere.DisplayMember = "Nom_Filiere";
            scom.CommandText = "select NUM_Groupe from Groupe where ID_Filiere= '" + comb_filiere.SelectedValue + "'";
            sda.SelectCommand = scom;
            sda.Fill(ds, "Groupe");
            comb_classe.DataSource = ds.Tables["Groupe"];
            comb_classe.ValueMember = "NUM_Groupe";
            //scom.CommandText = "select Etudiant.CNE from Etudiant inner join INSC_ET on Etudiant.CNE=INSC_ET.CNE inner join Inscription on Inscription.ID_Ins=INSC_ET.ID_Ins where INSC_ET.ID_Ins='" + comb_annee_sc.SelectedValue + "' and NUM_Groupe='" + comb_classe.SelectedValue + "'";
            //sda.SelectCommand = scom;
            //sda.Fill(ds, "Etudiant");
            //listBox1.DataSource = ds.Tables["Etudiant"]; 
            //listBox1.DisplayMember="CNE";
            //listBox1.ValueMember = "CNE";
            //scom.CommandText = "select Etudiant.CNE,Nom_ET,Prenom_ET ,Date_N ,Sexe_ET ,Tel_ET ,Ville_ET,Photo from Etudiant inner join INSC_ET on Etudiant.CNE=INSC_ET.CNE inner join Inscription on Inscription.ID_Ins=INSC_ET.ID_Ins where INSC_ET.ID_Ins='" + comb_annee_sc.SelectedValue + "' and NUM_Groupe='" + comb_classe.SelectedValue + "'";
            //sdr = scom.ExecuteReader();
            //while (sdr.Read())
            //{
            //    txt_cne.Text = sdr[0].ToString();
            //    txt_nom.Text = sdr[1].ToString();
            //    txt_prenom.Text = sdr[2].ToString();
            //    txt_daten.Text = sdr[3].ToString();
            //    txt_sexe.Text = sdr[4].ToString();
            //    txt_tele.Text = sdr[5].ToString();
            //    txt_ville.Text = sdr[6].ToString();
            //    txt_pic.Text = sdr[7].ToString();
                
            //}
            //sdr.Close();
            //listBox1.SelectedIndex = 0;
            //comb_annee_sc.SelectedIndexChanged -= comb_annee_sc_SelectedIndexChanged;

            if (txt_pic.Text == "")
                pictureBox1.Load("photos/vide.png");
            else
                pictureBox1.Load("photos/" + txt_pic.Text );
        }

        private void btn_Effacer_Click(object sender, EventArgs e)
        {
            txt_cne.Clear();
            txt_nom.Clear();
            txt_prenom.Clear();
            txt_sexe.Clear();
            txt_tele.Clear();
            txt_ville.Clear();
           
            txt_pic.Clear();
        }

        private void btn_valider_Click(object sender, EventArgs e)
        {
            scom.CommandText="insert into Etudiant values('"+txt_cne.Text+"','"+txt_nom.Text+"','"+txt_prenom.Text+"','"+dateTimePicker1.Value+"','"+txt_sexe.Text+"','"+txt_tele.Text+"','"+txt_ville.Text+"','"+comb_classe.SelectedValue+"','"+txt_pic.Text+"')";
            scom.ExecuteNonQuery();
            scom.CommandText = "insert into INSC_ET values('" + comb_annee_sc.SelectedValue + "','" + txt_cne.Text + "')";
            scom.ExecuteNonQuery();
            panel1.Visible = false;
            MessageBox.Show("votre inscription est bien passé");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

       


        private void btn_next_Click(object sender, EventArgs e)
        {
            //if (listBox1.SelectedIndex != -1)
            //{
            //    if (listBox1.SelectedIndex > 0)
            //        listBox1.SelectedIndex = listBox1.SelectedIndex -1;
            //}
        }

        private void btn_prec_Click(object sender, EventArgs e)
        {
            //if (listBox1.SelectedIndex != -1)
            //{
            //    if (listBox1.SelectedIndex < listBox1.Items.Count-1)
            //        listBox1.SelectedIndex = listBox1.SelectedIndex +1;
            //}

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //scom.CommandText = "select Etudiant.CNE,Nom_ET,Prenom_ET ,Date_N ,Sexe_ET ,Tel_ET ,Ville_ET,Photo from Etudiant inner join INSC_ET on Etudiant.CNE=INSC_ET.CNE inner join Inscription on Inscription.ID_Ins=INSC_ET.ID_Ins where INSC_ET.ID_Ins='" + comb_annee_sc.SelectedValue + "' and Etudiant.CNE='" + listBox1.SelectedValue + "' and NUM_Groupe=" + comb_classe.SelectedValue;
            //sdr = scom.ExecuteReader();
            //while (sdr.Read())
            //{

            //    txt_cne.Text = sdr[0].ToString();
            //    txt_nom.Text = sdr[1].ToString();
            //    txt_prenom.Text = sdr[2].ToString();
            //    txt_daten.Text = sdr[3].ToString();
            //    txt_sexe.Text = sdr[4].ToString();
            //    txt_tele.Text = sdr[5].ToString();
            //    txt_ville.Text = sdr[6].ToString();
            //    txt_pic.Text = sdr[7].ToString();
            //}
            //sdr.Close();

            //if (txt_pic.Text == "")
            //    pictureBox1.Load("photos/vide.png"); 
            //else
            //    pictureBox1.Load("photos/" + txt_pic.Text);
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

     

        private void comb_annee_sc_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //scom.CommandText = "select Etudiant.CNE,Nom_ET,Prenom_ET ,Date_N ,Sexe_ET ,Tel_ET ,Ville_ET from Etudiant inner join INSC_ET on Etudiant.CNE=INSC_ET.CNE inner join Inscription on Inscription.ID_Ins=INSC_ET.ID_Ins where Inscription.ID_Ins=" + comb_annee_sc.SelectedValue;
            //sdr = scom.ExecuteReader();
            //while (sdr.Read())
            //{

            //    txt_cne.Text = sdr[0].ToString();
            //    txt_nom.Text = sdr[1].ToString();
            //    txt_prenom.Text = sdr[2].ToString();
            //    txt_daten.Text = sdr[3].ToString();
            //    txt_sexe.Text = sdr[4].ToString();
            //    txt_tele.Text = sdr[5].ToString();
            //    txt_ville.Text = sdr[6].ToString();
            //}
            //sdr.Close();
        }

        private void comb_filiere_SelectionChangeCommitted(object sender, EventArgs e)
        {

            //if (comb_filiere.SelectedIndex != -1)
            //{

            //    scom.CommandText = "select NUM_Groupe from Groupe where ID_Filiere= " + comb_filiere.SelectedValue;
            //    sda.SelectCommand = scom;
            //    sda.Fill(ds, "Groupe");
            //    comb_classe.DataSource = ds.Tables["Groupe"];
            //    comb_classe.ValueMember = "NUM_Groupe";
            //    ds.Tables["Groupe"].Clear();
            //    scom.CommandText = "select NUM_Groupe from Groupe where ID_Filiere= " + comb_filiere.SelectedValue;
            //    sda.SelectCommand = scom;
            //    sda.Fill(ds, "Groupe");
            //    comb_classe.DataSource = ds.Tables["Groupe"];
            //    comb_classe.ValueMember = "NUM_Groupe";
            //}
            

        }

        private void comb_annee_sc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "image files|*.jpg;*.png;*.gif";
                DialogResult dr = ofd.ShowDialog();

                if (dr == DialogResult.Cancel)
                    return;

                pictureBox1.Image = Image.FromFile(ofd.FileName);
                txt_pic.Text = ofd.FileName;


            }



        }
    }
}
